<template>
  <div style="height: 6vh;background-color:#fff;padding: 10px 20px;">
    <span style="line-height:60px;font-size:20px;">会员管理</span>
    <el-button type="primary" style="float: right;margin-top: 10px;"
               @click="beforeAddJob">新建信息</el-button>
  </div>
  <el-dialog
      :title="dialogTitle"
      style="width: 1000px;padding: 40px;"
      v-model="dialogVisible"
      :before-close="handleClose"
  >
    <el-form label-position="top" label-width="80px">
      <el-row :gutter="10">
        <el-col :span="12">
          <el-form-item label="会员名">
            <el-input placeholder="请输入会员姓名" v-model="saveUserForm.username"
                      :disabled="saveUserForm.id != null"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="会员等级">
            <el-input placeholder="请输入会员等级" v-model="saveUserForm.salary"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="10">
        <el-col :span="12">
          <el-form-item label="电话">
            <el-input placeholder="请输入会员联系方式" v-model="saveUserForm.salary"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="注册店铺">
            <el-select placeholder="请选择" v-model="saveUserForm.shopAddr" >
              <el-option v-for="item in levelOptions"
                         :label="item.label" :value="item.value"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="10">
        <el-col :span="12">
          <el-form-item label="会员福利">
            <el-input v-model="saveUserForm.salary"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="备注消息">
            <el-input placeholder="请输入备注" v-model="saveUserForm.salary"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="10">
        <el-col :span="12">
          <el-form-item label="注册时间">
            <el-date-picker v-model="saveUserForm.entryTime" placeholder="请选择注册时间"></el-date-picker>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <template #footer>
      <el-button @click="handleClose">取消</el-button>
      <el-button type="primary" @click="saveJob">保存</el-button>
    </template>
  </el-dialog>

  <!-- 用户搜索卡片 -->
  <el-card style="margin: 20px;height: 70px;">
    <el-form :inline="true">
      <el-form-item label="电话" style="width: 220px;">
        <el-input placeholder="输入手机号" v-model="searchJobForm.username" @keydown.enter="loadJob"></el-input>
      </el-form-item>
      <el-form-item label="姓名" style="width: 220px;">
        <el-input placeholder="输入姓名" v-model="searchJobForm.username" @keydown.enter="loadJob"></el-input>
      </el-form-item>
      <el-form-item label="注册店铺" style="width: 220px;">
        <el-select placeholder="请选择" v-model="searchJobForm.jobAddr" >
          <el-option v-for="item in levelOptions"
                     :label="item.label" :value="item.value"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button @click="resetSearch">重置</el-button>
        <el-button type="primary" @click="loadJob">查询</el-button>
      </el-form-item>
    </el-form>
  </el-card>

  <!-- 用户表格卡片 -->
  <el-card style="margin: 20px;">
    <el-table :data="jobArr">
      <el-table-column type="index" label="编号" width="80" align="center"></el-table-column>
      <el-table-column prop="name" label="会员名" align="center"></el-table-column>
      <el-table-column prop="phone" label="联系方式" align="center"></el-table-column>
      <el-table-column prop="loginTime" label="注册" align="center"></el-table-column>
      <el-table-column prop="updateTime" label="更新时间" align="center"></el-table-column>
      <el-table-column prop="welfare" label="会员福利" align="center"></el-table-column>
      <el-table-column  prop="addr" label="注册店铺" align="center"></el-table-column>
      <el-table-column prop="remark" label="备注" align="center">
        <template #default="scope">
          <el-switch active-value="1" inactive-value="0"
                     v-model="scope.row.status"
                     @change="changeStatus(scope.row.id,scope.row.status)"
          ></el-switch>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center">
        <template #default="scope">
          <el-button link type="primary" :disabled="scope.row.level==40"
                     @click="editJob(scope.row.id)">编辑</el-button>
          <el-button link type="primary" :disabled="scope.row.level==40"
                     @click="deleteJob(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
  </el-card>
</template>
<script setup>
import {onMounted, ref} from "vue";
import {ElMessage} from "element-plus";
import axios from "axios";
import qs from "qs";
import index from "vuex";

const dialogVisible = ref(false);
//定义变量用来控制弹窗的标题
const dialogTitle = ref('新建员工工作信息');

const levelOptions = ref([
]);
const saveUserForm = ref({

});
const beforeAddJob = ()=>{
  dialogVisible.value = true;
  dialogTitle.value = '新建用户';
}
const titleFormatter=(row,column,cellValue,index)=>{
  for (let item of levelOptions.value) {
    if (item.value==cellValue) {
      cellValue = item.label;
    }
  }
  return cellValue;
}

const saveJob = ()=>{
  let data = qs.stringify(saveUserForm.value);
  console.log(data)
  axios.post(BASE_URL+'/v1/job/save',data)
      .then((response)=>{
        if(response.data.code == 2000){
          ElMessage.success('保存成功!');
          dialogVisible.value = false;
          saveUserForm.value = {};
          loadJob();
        }else{
          ElMessage.error(response.data.msg);
        }
      })
}
const handleClose = ()=>{
  if(confirm('您确认要关闭本窗口吗?')){
    dialogVisible.value = false;
    saveUserForm.value = {};
  }
}
const searchJobForm = ref({username:'', status:'',jobAddr:''});
const jobArr = ref([]);
const loadJob = ()=>{
  let data = qs.stringify(searchJobForm.value);
  axios.get(BASE_URL+'/v1/job/select?'+data)
      .then((response)=>{
        if(response.data.code == 2000){
          jobArr.value = response.data.data;
        }else{
          ElMessage.error(response.data.msg);
        }
      })
}
const resetSearch = ()=>{
  searchJobForm.value = {};
  loadJob();
}
// onMounted(()=>{
//   loadJob();
// })
const editJob = (id)=>{
  dialogVisible.value = true;
  dialogTitle.value = '编辑员工工作信息';
  axios.get(BASE_URL+'/v1/job/select?id='+id).then((response)=>{
    if(response.data.code == 2000){
      saveUserForm.value = response.data.data[0];
    }else{
      ElMessage.error(response.data.msg);
    }
  })
}
const changeStatus = (id,status)=>{
  axios.post(BASE_URL+'/v1/job/update/status/'+id+'/'+status)
      .then((response)=>{
        if(response.data.code == 2000){
          ElMessage.success('修改状态成功!');
        }else{
          ElMessage.error(response.data.msg);
        }
      })
}
const deleteJob = (id)=>{
  if(confirm('您确认要删除该员工信息吗?')){
    axios.post(BASE_URL+'/v1/job/delete/'+id)
        .then((response)=>{
          if(response.data.code == 2000){
            ElMessage.success('删除成功!');
            loadJob();
          }else{
            ElMessage.error(response.data.msg);
          }
        })
  }
}
</script>


<style scoped>

</style>